<template>
  <div class="app">
    <el-alert v-show="showNotice"
      title="更新成功"
      type="success"
      show-icon style="margin-bottom:20px;">
    </el-alert>
    <el-form ref="form" :model="user_info" label-width="120px">
      <el-form-item label="昵称">
        <el-input v-model="user_info.nickname"></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio v-model="user_info.gender" label="1">男</el-radio>
        <el-radio v-model="user_info.gender" label="2">女</el-radio>
      </el-form-item>
      <el-form-item label="用户类型">
        <el-select v-model="user_info.user_type">
          <el-option label="未知类型" value="0">未知类型</el-option>
          <el-option label="普通用户" value="1">普通用户</el-option>
          <el-option label="VIP会员" value="2">VIP会员</el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="等级">
        <el-input v-model="user_info.level"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" v-on:click="updateHandler">修改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getUserInfo, editUserInfo } from '@/api/user'
export default {
  name: 'UserEditor',
  data: function() {
    // 属性
    return {
      showNotice: false,
      user_info: {}
    }
  },
  props: [
    'uid'
  ],
  watch: {
    uid: function(val, oldVal) {
      console.log('load user info ...')
      if (val > 0) {
        this.loadUserInfo(val)
      }
    }
  },
  created: function() {
    if (this.uid > 0) {
      this.loadUserInfo(this.uid)
    }
  },
  methods: {
    /** 组件使用的方法 */
    loadUserInfo(uid) {
      // 载入用户信息
      getUserInfo(uid).then(response => {
        console.log(response)
        this.user_info = response.data.user_info
      })
    },

    updateHandler() {
      const param = {
        target_uid : this.uid,
        level : this.user_info.level,
        gender : this.user_info.gender,
        nickname : this.user_info.nickname,
        user_type : this.user_info.user_type
      }
      editUserInfo(param).then(response => {
        console.log(response)
        if (response.code === 0) {
          this.showNotice = true
        }
      })
    }
  }
}
</script>

